package com.chensi.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chensi.common.AjaxJson;
import com.chensi.common.Pager;
import com.chensi.model.Node;
import com.chensi.service.INodeService;

@Controller
@RequestMapping("node")
public class NodeController {
	
	@Autowired
	private INodeService nodeService;
	
	@RequestMapping("addView")
	public ModelAndView addView(Integer flowId) {
		Node entity =new Node();
		entity.setFlowId(flowId);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("node", entity);
		return new ModelAndView("flow/nodeEdit",map);
	}
	
	@RequestMapping("updateView")
	public ModelAndView updateView(Node node) {
		Node entity =nodeService.get(node.getId());
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("node", entity);
		return new ModelAndView("flow/nodeEdit",map);
	}
	
	//---------------------

	@RequestMapping("page.do")
	@ResponseBody
	public AjaxJson page(Node node, Pager pager) {
		return AjaxJson.getSuccessInfo(nodeService.pageByEntity(node, pager));
	}
	
	@RequestMapping("update.do")
	@ResponseBody
	public AjaxJson update(Node node) {
		Node param=new Node();
		param.setIdNe(node.getId());
		param.setStep(node.getStep());
		param.setFlowId(node.getFlowId());
		if(nodeService.getByEntity(param)!=null){
			return AjaxJson.getFailInfo("同一流程不可存在相同阶段");
		}
		nodeService.save(node);
		return AjaxJson.getSuccessInfo();
	}
	
	@RequestMapping("add.do")
	@ResponseBody
	public AjaxJson add(Node node) {
		Node param=new Node();
		param.setStep(node.getStep());
		param.setFlowId(node.getFlowId());
		if(nodeService.getByEntity(param)!=null){
			return AjaxJson.getFailInfo("同一流程不可存在相同阶段");
		}
		nodeService.save(node);
		return AjaxJson.getSuccessInfo();
	}
	
}
